package com.cncf.controller.manage;

import com.cncf.entity.Report;
import com.cncf.response.ResponseData;
import com.cncf.service.ReportService;
import com.cncf.util.UploadFileUtil;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author zj
 */
@Api(value = "Admin", description = "报告数据导入接口")
@Controller
@RequestMapping("/manage/report")
public class ManageReportController {
    private static final Log logger = LogFactory.getLog(ManageReportController.class);
    @Autowired
    private ReportService reportService;

    @ApiOperation(value = "解析服务器中的报告数据excel文件", notes = "解析之前要先将excel文件上传到服务器")
    @RequestMapping(value = "importReportExcel", method = {RequestMethod.POST})
    @ResponseBody
    public ResponseData<Boolean> importReportExcel(
            @ApiParam("excel报告文件url") @RequestParam("url") String url) throws IOException {

        String[] arr=url.split("/");
        url="/data/"+arr[4]+"/"+arr[5];
        logger.debug("url:"+url);

        ResponseData<Boolean> responseData = new ResponseData<>();
        logger.debug("111");
        File file = new File(url);
        String name = file.getName();
        logger.debug("222:"+name);
        long length = file.length();
        logger.debug("333:"+length);
        //String name = url.substring(url.lastIndexOf("/") + 1);// 从全路径中提取文件名
        //也许不会出现一下几种情况但是多判断一下有备无患
        if (name == null || "".equals(name) || length == 0) {
            logger.debug("444");
            return null;
        }

        InputStream in = new FileInputStream(file);
        boolean res = reportService.insertReportForBatch(name, in);
        if (!res) {
            responseData.jsonFill(2, "插入失败（插入数据库的记录数不等于表格中的记录数）", false);
            return responseData;
        }
        responseData.jsonFill(1, null, true);
        return responseData;
    }

/*    @ApiOperation(value = "获得某个企业的所有报告信息", notes = "")
    @RequestMapping(value = "getAllReportByEnterprise", method = {RequestMethod.GET})
    @ResponseBody
    public ResponseData<List<Report>> getAllReportByEnterprise(@ApiParam("企业ID") @RequestParam("id") String id){

    }*/

    @ApiOperation(value = "通过ID获取检验报告", notes = "")
    @RequestMapping(value = "getReportById", method = {RequestMethod.GET})
    @ResponseBody
    public ResponseData<Report> getReportById(@ApiParam("") @RequestParam("id") String id) {
        ResponseData<Report> responseData = new ResponseData<>();
        Report report = reportService.getReportById(id);
        if (report == null) {
            responseData.jsonFill(2, "无效的id", null);
            return responseData;
        }
        responseData.jsonFill(1, null, report);
        return responseData;
    }

    @ApiOperation(value = "获取所有的检验报告", notes = "不提供page和pageSize就不分页")
    @RequestMapping(value = "getAllReport", method = {RequestMethod.GET})
    @ResponseBody
    public ResponseData<List<Report>> getAllReport(
            @ApiParam("page") @RequestParam(value = "page", required = false) Integer page,
            @ApiParam("pageSize") @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        ResponseData<List<Report>> responseData = new ResponseData<>();
        List<Report> list = reportService.getAllReport(page, pageSize);
        responseData.jsonFill(1, null, list);
        responseData.setCount(list.size());
        return responseData;
    }

}
